package com.niit.bc.servlet;

import com.niit.bc.bean.UserBean;
import com.niit.bc.biz.UserBiz;
import com.niit.bc.biz.impl.UserBizImpl;
import com.niit.bc.util.web.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

public class Login extends HttpServlet {

    /**
     * The doGet method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=utf8");
        request.setCharacterEncoding("utf8");

        UserBean userBean = new UserBean();
        UserBiz userBiz = new UserBizImpl();

        //获取页面输入的用户名，密码，和记住信息，并封装到用户信息中

        String userName = request.getParameter("userName");
        String userPwd = request.getParameter("userPwd");

        if (userName != null && !userName.trim().equals("")) {
            userBean.setUserName(userName);
        }
        if (userPwd != null && !userName.trim().equals("")) {
            userBean.setUserPwd(userPwd);
        }

        String rem = request.getParameter("rem");

        //验证用户是否存在，若数据库存在该用户，则登录成功，跳转到主页面的servlet,并将userId,userName,userPwd存入cookie和session中

        if (userBean != null) userBean = userBiz.findUser(userBean);

        if (userBean.getUserId() > 0) {
            if (rem != null && !rem.trim().equals("")) {
                Cookie cookie1 = new Cookie(Constants.LOGIN_COOKIE_USERNAME, userName);
                Cookie cookie2 = new Cookie(Constants.LOGIN_COOKIE_USERPWD, userPwd);
                cookie1.setMaxAge(60 * 60 * 24 * 7);
                cookie2.setMaxAge(60 * 60 * 24 * 7);
                response.addCookie(cookie1);
                response.addCookie(cookie2);
            }
            HttpSession session = request.getSession();
            session.setAttribute(Constants.LOGIN_SESSION_USER, userBean);

            request.getRequestDispatcher("Index").forward(request, response);
        } else {
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
            out.println("<HTML>");
            out.println("  <HEAD><TITLE>登录失败</TITLE></HEAD>");
            out.println("  <BODY>");
            out.print("<SCRIPT language='javascript' >alert('登录失败')</SCRIPT>");
            out.println("重新");
            out.println("<a href='login.jsp'>");
            out.println("登录");
            out.println("</a>");
            out.println("  </BODY>");
            out.println("</HTML>");
            out.flush();
            out.close();
        }

    }

    /**
     * The doPost method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }
}
